/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/16  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.RecommendBook;
import com.sansi.pencilbook.mapper.RecommendBookMapper;
import com.sansi.pencilbook.service.RecommendBookServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能说明:
 *
 * @author lisy31662
 */
@Service
public class RecommendBookServerImpl implements RecommendBookServer {

  @Autowired
  private RecommendBookMapper recommendBookMapper;

  @Override
  public Result addRecommendBooks(List<RecommendBook> recommendBooks) {
    try{
      int num = recommendBookMapper.saveRecommendBooks(recommendBooks);
      return num>0 ? Result.ofSuccess("添加推荐书籍成功", num) :
          Result.ofFailed("添加推荐书籍失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result deleteRecommendBooksByUserId(int userId) {
    try{
      int num = recommendBookMapper.deleteRecommendBooksByUser(userId);
      return num>0 ? Result.ofSuccess("删除推荐书籍成功", num) :
          Result.ofFailed("删除推荐书籍失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result updateUserRecommend(int userId, List<RecommendBook> recommendBooks) {
    try {
      int del = recommendBookMapper.deleteRecommendBooksByUser(userId);
      int add = recommendBookMapper.saveRecommendBooks(recommendBooks);
      if (add<=0) {
        return Result.ofFailed("添加新推荐列表失败");
      }
      return Result.ofSuccess("更新用户推荐列表成功");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getRecommendBookByUser(int userId) {
    try {
      List<RecommendBook> list = recommendBookMapper.findRecommendBooksByUser(userId);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }
}
